package ui
{
	import data.GCBuildingData;
	
	import events.GCEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import ui.button.GCButton;
	import ui.textfield.GCStylizedText;
	
	/**
	 * A ui component that allows a user to increase and decrease a number
	 * via buttons to the right and left of a number field. Also contains a
	 * place to hold an image above it.
	 */
	public class GCNumberSelector extends Sprite
	{
		private static const IMAGE_DIMENSION:int = 120;
		private static const BUTTON_WIDTH:int = 30;
		private static const BUTTON_HEIGHT:int = 30;
		private static const MIN_VALUE:int = 0;
		
		private var _value:int;
		
		private var _buildingId:uint;
		
		private var _imageDisplay:Sprite;
		private var _buildingDisplay:GCStylizedText;
		private var _numberDisplay:GCStylizedText;
		private var _increaseButton:GCButton;
		private var _decreaseButton:GCButton;
		
		public function GCNumberSelector(buildingId:uint, x:int = 0, y:int = 0)
		{
			super();
			this.x = x;
			this.y = y;
			
			_value = 0;
			_buildingId = buildingId;
			_imageDisplay = new Sprite();
			_buildingDisplay = new GCStylizedText();
			_numberDisplay = new GCStylizedText();
			_increaseButton = new GCButton(onIncreaseValue);
			_decreaseButton = new GCButton(onDecreaseValue);
			
			init();
			
		}
		
		public function get value():int
		{
			return _value;
		}
		
		public function setValue(newValue:int):void
		{
			if (newValue >= MIN_VALUE)
			{
				_value = newValue;
			
				_numberDisplay.text = String(this._value);
				
				dispatchEvent(new GCEvent(GCEvent.VALUE_CHANGED_EVENT));
			}
		}
		
		public function setEnableIncrease(enable:Boolean):void
		{
			_increaseButton.mouseEnabled = enable;
		}
		
		public function setEnableDecrease(enable:Boolean):void
		{
			_decreaseButton.mouseEnabled = enable;
		}
		
		public function setImage():void
		{
			
		}
		
		public function setToolTip():void
		{
			
		}
		
		private function init():void
		{
			_numberDisplay.text = String(this._value);
			_buildingDisplay.text = GCBuildingData.getNameFromId(_buildingId);
			
			_imageDisplay.addChild(GCResources.getResource(GCBuildingData.getNameFromId(_buildingId)));
			_imageDisplay.width = IMAGE_DIMENSION;
			_imageDisplay.height = IMAGE_DIMENSION;
			
			var yOffset:int = _imageDisplay.y + _imageDisplay.height;

			_decreaseButton.y = yOffset;
			_decreaseButton.loadGraphic(GCResources.left_minus_arrow, 35, 35);
			
			
			_numberDisplay.x = _decreaseButton.x + _decreaseButton.width;
			_numberDisplay.y = yOffset;
			_numberDisplay.setBgStyle(GCStylizedText.STYLE_ONE, IMAGE_DIMENSION - BUTTON_WIDTH * 2, BUTTON_HEIGHT);
			_buildingDisplay.setBgStyle(GCStylizedText.STYLE_TWO, IMAGE_DIMENSION, BUTTON_HEIGHT);
			_buildingDisplay.y = _numberDisplay.y + _numberDisplay.height;
			
			_increaseButton.x = _numberDisplay.x + _numberDisplay.width;
			_increaseButton.y = yOffset;
			_increaseButton.loadGraphic(GCResources.right_plus_arrow, 35, 35);
			
			this.setEnableIncrease(true);
			this.setEnableDecrease(true);
			
			this.addChild(_imageDisplay);
			this.addChild(_numberDisplay);
			this.addChild(_buildingDisplay);
			this.addChild(_increaseButton);
			this.addChild(_decreaseButton);
		}
		
		private function onIncreaseValue(event:MouseEvent):void
		{
			this.setValue(_value + 1);
			
		}
		
		private function onDecreaseValue(event:MouseEvent):void
		{
			this.setValue(_value - 1);
		}
	}
}